package aooO.crm.Controller;

import aooO.crm.Service.CustomerRemarkService;
import aooO.crm.Service.CustomerService;
import aooO.crm.Service.UserService;
import aooO.crm.common.Utils.DateUtils;
import aooO.crm.common.Utils.UUIDUtils;
import aooO.crm.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("workbench/customer")
public class customerController {

    @Autowired
    CustomerService customerService;

    @Autowired
    UserService userService;

    @Autowired
    CustomerRemarkService customerRemarkService;

//    workbench/customer/index.do
    @RequestMapping("/index.do")
    public String index(HttpServletRequest  request){
        //分页查询所有的联系人
        List<User> userList = userService.selectAllUser();
        request.setAttribute("userList",userList);
//        List<Customer> customerList = customerService.selectCustomerByConditionForPage();
        return "/workbench/customer/index";
    }

//    workbench/customer/queryCustomerByConditionForPage.do
    @RequestMapping("/queryCustomerByConditionForPage.do")
    @ResponseBody
    public Object queryCustomerByConditionForPage(String name,String owner,String phone,String website,int pageNo,int pageSize){
       /* name:name,
        owner:owner,
        phone:phone,
        website:website,
        pageNo:pageNo,
        pageSize:pageSize*/
        Map<String,Object> map= new HashMap<>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("phone",phone);
        map.put("website",website);
        int beginNo=(pageNo-1)*pageSize;
        map.put("beginNo",beginNo);
        map.put("pageSize",pageSize);
        List<Customer> customerList = customerService.selectCustomerByConditionForPage(map);
        Map<String,Object> data=new HashMap<>();
        data.put("customerList",customerList);
        int totalRows=customerService.selectTotalPage(map);
        data.put("totalRows",totalRows);
        return data;
    }

//    'workbench/customer/saveCreateCustomer.do'
    @RequestMapping("/saveCreateCustomer.do")
    @ResponseBody
    public Object saveCreateCustomer(Customer customer,HttpServletRequest request){
        /*owner:owner,
        name:name,
        website:website,
        phone:phone,
        description:description,
        contactSummary:contactSummary,
        nextContactTime:nextContactTime,
        address:address*/
        returnObject data=new returnObject(stateCode.STATE_FAILURE,"");
        customer.setId(UUIDUtils.uuid());
        User user = (User) request.getSession().getAttribute(stateCode.SESSION_USER);
        customer.setCreateBy(user.getId());
        customer.setCreateTime(DateUtils.FormatDateTime(new Date()));
        try {
            int i = customerService.insertCustomer(customer);
            if(i>0){
                data.setCode(stateCode.STATE_SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            data.setMessage("保存失败，请重试！");
        }
        return data;
    }


//    'workbench/customer/queryCustomerById.do'
     @RequestMapping("/queryCustomerById.do")
     @ResponseBody
     public Object queryCustomerById(String id) {
         return customerService.selectCustomerById(id);
     }


//    workbench/customer/saveEditCustomer.do'
     @RequestMapping("/saveEditCustomer.do")
     @ResponseBody
     public Object saveEditCustomer(Customer customer, HttpSession session) {
         User user = (User) session.getAttribute(stateCode.SESSION_USER);
         // 补充参数
         customer.setEditBy(user.getId());
         customer.setEditTime(DateUtils.FormatDateTime(new Date()));
         returnObject returnObject = new returnObject(stateCode.STATE_FAILURE,"");
         try {
             // 保存更新的对应id的客户
             int res = customerService.updateCustomer(customer);
             if (res > 0) { // 更新成功
                 returnObject.setCode(stateCode.STATE_SUCCESS);
             } else { // 更新失败，服务器端出了问题，为了不影响顾客体验，最好不要直接说出问题
                 returnObject.setMessage("系统繁忙，请稍后重试...");
             }
         } catch (Exception e) { // 发生了某些异常，捕获后返回信息
             e.printStackTrace();
             returnObject.setCode(stateCode.STATE_FAILURE);
             returnObject.setMessage("系统繁忙，请稍后重试...");
         }
         return returnObject;
     }

    @RequestMapping("/deleteCustomerByIds.do")
    @ResponseBody
    public Object deleteCustomerByIds(String[] id) {
        returnObject returnObject = new returnObject(stateCode.STATE_FAILURE,"");
        try {
            customerService.deleteCustomerByIds(id); // 删除客户以及市场活动所绑定的所有信息
            returnObject.setCode(stateCode.STATE_SUCCESS);
        } catch (Exception e) { // 发生了某些异常，捕获后返回信息
            e.printStackTrace();
            returnObject.setCode(stateCode.STATE_FAILURE);
            returnObject.setMessage("系统繁忙，请稍后重试...");
        }
        return returnObject;
    }


//    'workbench/customer/detailCustom.do?id="
    @RequestMapping("/detailCustom.do")
    public String detailCustom(String id,HttpServletRequest request){
        //查询所有的客户信息详情和 备注信息
        Customer customer = customerService.selectCustomerDetailById(id);
        List<CustomerRemark> customerRemarkList = customerRemarkService.selectRemarkByCustomerId(id);
        request.setAttribute("customer", customer);
        request.setAttribute("customerRemarkList", customerRemarkList);
        return "/workbench/customer/detail";
    }
}
